package com.howells.time.module.sendvideo.presenter;


import com.alibaba.fastjson.JSON;
import com.howells.time.base.http.HttpURL;
import com.howells.time.base.http.HttpUtils;
import com.howells.time.base.http.JsonCallback;
import com.howells.time.module.sendvideo.view.SendVideoView;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.Map;

import okhttp3.Call;


/**
 * Created by Administrator on 2016.4.12.
 */
public class SendVideoPresenterImp implements SendVideoPresenter {
    private SendVideoView sendVideoView;

    public SendVideoPresenterImp(SendVideoView sendVideoView) {
        this.sendVideoView = sendVideoView;
    }

    @Override
    public void uploadvideo(String filepath, final String picpath, Map info) {
        HttpUtils.UploadFile(HttpURL.UploadVideoApi.toString(), new File(filepath), info, new JsonCallback() {

            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                sendVideoView.resultMsg("网络错误");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response.get("message").equals("success")) {
                    Logger.d(response);
                    Map<String,Object> info=JSON.parseObject(response.get("data").toString());
                    Logger.d(info);
                    HttpUtils.UploadFile(HttpURL.UploadVideoPicApi.toString(), new File(picpath), info, new JsonCallback() {
                        @Override
                        public void onError(Call call, Exception e,int id) {
                            sendVideoView.resultMsg("网络异常");
                        }

                        @Override
                        public void onResponse(Map<String, Object> response,int id) {
                            if(response.get("message").equals("success")){
                                sendVideoView.sendsuccess();
                            }else {
                                Logger.d(response);
                                sendVideoView.resultMsg(response.toString());
                            }
                        }
                    });
                }
                else {
                    Logger.d(response);
                    sendVideoView.resultMsg(response.toString());
                }
            }
        });
    }

    @Override
    public void getMode() {
        HttpUtils.Get(HttpURL.GetModeApi.toString(), new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                sendVideoView.resultMsg("网络错误");
            }

            @Override
            public void onResponse(Map<String, Object> response, int id) {
                if(response.get("message").equals("success")){
                    sendVideoView.selectmode(response.get("data"));
                }else {
                    Logger.d(response);
                }
            }
        });
    }
}
